using BehaviorDesigner.Runtime;
using BehaviorDesigner.Runtime.Tasks;
using QFramework;
using UnityEngine;
using System;
using System.Reflection;

namespace HareGame
{
    public enum UIFingerType
    {
        None,
        Mask,
        UIItem,
    }

    [TaskCategory("引导")]
    public class ShowUIFinger : BehaviorDesigner.Runtime.Tasks.Action
    {
        [Header("手指跟随的目标对象")]
        public SharedGameObject targetObj;

        [Header("手指跟随目标名字(targetObj为空时生效)")]
        public string targetName;

        [Header("遮照类型")]
        public UIFingerType maskType = UIFingerType.Mask;

        [Header("手指偏移值")]
        public Vector2 offset;

        [Header("是否为世界坐标(3D坐标)")]
        public bool isWorldPoint = false;

        [Header("类名")]
        public string className;

        [Header("接口名字(不能带参数)")]
        public string apiName;

        [Header("需要点击的次数")]
        public int clickTimes = 1;

        UIFinger _uiFinger;
        private bool _isReady;
        private Canvas _canvas;

        public override void OnStart()
        {
            base.OnStart();

            _isReady = false;

            if (targetObj.Value == null)
            {
                targetObj.Value = GameObject.Find(targetName);
                if (targetObj.Value == null)
                {
                    LogKit.E("找不到名为:" + targetName + "的对象");
                    return;
                }
            }

            Type classType = Type.GetType(className);
            if (classType == null)
            {
                LogKit.E("不存在名为:" + className + "的类");
                return;
            }

            MethodInfo method = classType.GetMethod(apiName);
            if (method == null)
            {
                LogKit.E(className + "中不存在名为:" + apiName + "的接口");
                return;
            }

            Component component = targetObj.Value.GetComponentInParent(classType);
            if (component == null)
            {
                LogKit.E("不存在包含" + className + "的对象");
                return;
            }

            _isReady = true;

            _uiFinger = GameEntry.UI.OpenUI<UIFinger>(UILevel.CanvasPanel, new UIFingerData()
            {
                Pos = targetObj.Value.transform.position,
                Offset = offset,
                ClickTimes = clickTimes,
                IsWorldPoint = isWorldPoint,
                MaskType = maskType,
                OnClick = () =>
                  {
                      method.Invoke(component, null);
                  }
            });

            if (maskType == UIFingerType.UIItem)
            {
                _canvas = targetObj.Value.GetOrAddComponent<Canvas>();
                _canvas.overrideSorting = true;
                _canvas.sortingOrder = 550;
            }
        }

        public override TaskStatus OnUpdate()
        {
            if (_isReady && _uiFinger == null)
            {
                if (_canvas != null)
                    GameObject.Destroy(_canvas);
                return TaskStatus.Success;
            }
            return TaskStatus.Running;
        }
    }
}